const app = getApp()
const adapters = require("../../utils/adapters");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    header: {
      title: '质保中心',
      whiteBg: false,
      mode: 'title',
      navBarHeight: app.globalData.navBarHeight,
      tabbarHeight: app.globalData.tabbarHeight,
      screenHeight: app.globalData.screenHeight,
    },
    selectItem: {
      outerIndex: -1,
      index: -1,
    },
    workOrderList: [],
    tabBarType: ['全部', '已处理', '待处理'],
    searchBar: {
      value: ''
    }
  },

  onPageScroll(e) {
    this.setData({
      'header.whiteBg': e.scrollTop < 40 ? false : true
    })
  },

  searchBarOnChange(e) {
    this.setData({
      "searchBar.value": e.detail
    })
  },

  searchBarOnSearch() {
    let workOrderList = this.data.workOrderList
    const target = this.data.searchBar.value
    workOrderList.forEach(workOrder => {
      workOrder.show = workOrder.maintenanceNumber.includes(target)
    })
    this.setData({
      workOrderList: workOrderList
    })
  },

  navigatePage(e) {
    const pageName = e.currentTarget.dataset.pagename
    const maintenanceNumber = e.currentTarget.dataset.maintenancenumber
    wx.navigateTo({
      url: '/pages/' + pageName + '/' + pageName + '?maintenanceNumber=' + maintenanceNumber,
    })
  },

  selectItem(e) {
    const outerIndex = e.currentTarget.dataset.outerindex
    const newSelectIndex = e.currentTarget.dataset.index
    const oldSelectIndex = this.data.selectItem.index
    const buttonShow = this.data.selectItem.buttonShow
    let resIndex
    if (newSelectIndex === oldSelectIndex) {
      resIndex = -1
    } else {
      resIndex = newSelectIndex
    }
    this.setData({
      'selectItem.index': resIndex,
      'selectItem.outerIndex': outerIndex,
    })

  },

  resetSelectItem() {
    this.setData({
      'selectItem.index': -1,
      'selectItem.outerIndex': -1,
    })
  },
  
  async onLoad(options) {
    const getWorkOrderListRes = await adapters.getWorkOrderList();
    this.setData({
      workOrderList: getWorkOrderListRes.data
    })
  },

})